package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/list")
public class ListApiController {
    /*    @Autowired
    private TransportClient transportClient;//低级别客户端   早期*/
    @Autowired
    private RestHighLevelClient restHighLevelClient;//高级别客户端   最近
    /*    @Autowired
        private ElasticsearchTemplate elasticsearchTemplate;//低级别客户端   早期*/
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//高级别客户端   最近

    @Autowired
    private ListService listService;
    //创建索引库 Document feild类型
    @GetMapping("/createIndex")
    public Result createIndex(){
        //创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //mapping映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //上架
    @PostMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @PostMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listService.cancelSale(skuId);
        return Result.ok();
    }

    //商品热度评分 (加分接口)
    @PutMapping("/inner/incrHotScore/{skuId}/{score}")
    public void incrHotScore(@PathVariable Long skuId,@PathVariable Integer score){
        listService.incrHotScore(skuId,score);
    }

    //开始搜索
    @PostMapping("/inner/search")
    public SearchResponseVo search(@RequestBody SearchParam searchParam){
        return listService.search(searchParam);
    }



}
